package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.FollowedBaseInfoMergeRequest;
import com.frogs.ucenter.v1.request.FollowedBaseInfoRequest;
import com.frogs.ucenter.v1.request.UserBaseInfoMergeRequest;
import com.frogs.ucenter.v1.request.UserBaseInfoQueryRequest;
import com.frogs.ucenter.v1.request.UserInfoCheckRequest;
import com.frogs.ucenter.v1.service.UserBaseInfoService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION+"UserBaseInfoController")
@RequestMapping("/"+ApiConstants.VERSION)
public class UserBaseInfoController extends BaseController3 {
	
	@Resource(name=ApiConstants.VERSION + "userBaseInfoService")
	private UserBaseInfoService userBaseInfoService;
	
    private static final String ROOT = "/user";
    private static final String USER_BASEINFO_MERGE = ROOT + "/baseinfo" + MERGE;
    
    private static final String USER_BASEINFO_DETAIL = ROOT + "/baseinfo" + DETAIL;
    private static final String USER_BASEINFO_CHECK = ROOT + "/baseinfo" + "/check";
    
    private static final String FOLLOWED_BASE_INFO_UPDATE = "/followed/baseinfo" + MERGE;
    private static final String FOLLOWED_INFO = "/followed/info";
	/**
	 * 本接口实现的功能为检索库中存在的记录，若存在，执行更新操作，否则做插入操作,
	 * 由于用户信息跟账户信息一一对应，所以用户信息在逻辑上不存在插入操作，uid为必要字段
	 * @param request
	 * @param userBaseInfo
	 * @return
	 */
	@RequestMapping(USER_BASEINFO_MERGE)
	public Response userBaseInfoMerge(HttpServletRequest request) {
		UserBaseInfoMergeRequest userRequest = new UserBaseInfoMergeRequest(); 
		Response response;
		try {
			userRequest.parse(request);
			response = userBaseInfoService.mergeUserBean(userRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	/**
	 * 未知用户的信息修改
	 * @param request
	 * @return
	 */
	@RequestMapping(FOLLOWED_BASE_INFO_UPDATE)
	public Response followedBaseInfoMerge(HttpServletRequest request) {
		FollowedBaseInfoMergeRequest followedBaseInfoMergeRequest = new FollowedBaseInfoMergeRequest(); 
		Response response;
		try {
			followedBaseInfoMergeRequest.parse(request);
			response = userBaseInfoService.mergeFollowedBean(followedBaseInfoMergeRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	/**
	 * 未知用户的信息获取
	 * @param request
	 * @return
	 */
	@RequestMapping(FOLLOWED_INFO)
	public Response followedBaseInfo(HttpServletRequest request) {
		FollowedBaseInfoRequest followedBaseInfoRequest = new FollowedBaseInfoRequest(); 
		Response response;
		try {
			followedBaseInfoRequest.parse(request);
			response = userBaseInfoService.getFollowedInfo(followedBaseInfoRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping({USER_BASEINFO_DETAIL})
	public Response userBaseInfoDetail(HttpServletRequest request) {
		UserBaseInfoQueryRequest userRequest = new UserBaseInfoQueryRequest(); 
		Response response;
		try {
			userRequest.parse(request);
			response = userBaseInfoService.getUserBean(userRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(USER_BASEINFO_CHECK)
	public Response userInfoCheck(HttpServletRequest httpRequest) {
		Request request = new UserInfoCheckRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userBaseInfoService.checkUserInfo(request);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
}
